//
//  TKIMEmotionView.m
//  TKIM
//
//  Created by liupm on 15/8/3.
//  Copyright (c) 2015年 liupm. All rights reserved.
//

#import "TKIMEmotionView.h"

@implementation TKIMEmotionView
{
    NSMutableArray *_dataArray;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initData];
    }
    return self;
}


/**
 *  @Author lpm, 15-08-03 14:08:18
 *
 *  @brief 初始化表情数据
 */
-(void)initData
{
    _dataArray = [self getConfigDataByKey:@"emotion"];
    [self drawView:_dataArray];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(self.width-80, self.height-40, 70, 35)];
    button.backgroundColor = [TKIMColorHelper stringToColor:@"#157EFB"];
    [button setTitle:@"发送" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor]  forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor]  forState:UIControlStateHighlighted];
    button.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    button.layer.cornerRadius = 5.0f;
    button.layer.shadowColor = [UIColor grayColor].CGColor;
    button.layer.shadowOffset = CGSizeMake(0, 1);
    button.layer.shadowRadius = 1.0f;
    button.layer.shadowOpacity = 1.0;
    
    [button addTarget:self action:@selector(send) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
}


/**
 *  @Author lpm, 15-08-03 16:08:19
 *
 *  @brief  获得子元素
 *
 */
-(UIView *)getItem:(NSMutableDictionary *)data frame:(CGRect)rect
{
    NSString *imgUrl = data[@"value"];
    UIImage *image = TKIMGetImage(imgUrl);
    if (!image) {
        return nil;
    }
    UIButton *button = [[UIButton alloc] initWithFrame:rect];
    [button setImage:image forState:UIControlStateNormal];
    button.backgroundColor = [UIColor clearColor];
    button.imageEdgeInsets = UIEdgeInsetsMake(8, 8, 8, 8);
    return button;
}


/**
 *  @Author lpm, 15-08-03 16:08:19
 *
 *  @brief  子元素被点击
 *
 */
-(void)item:(UIView *)view didSelected:(NSInteger)index
{
    NSMutableDictionary *dic = _dataArray[index];
    NSString *key = dic[@"key"];
    if ([key isEqualToString:@"delete"]){
        [self routerEventWithName:TKIMEmotionDeleteEvent userInfo:dic];
    }else{
        [self routerEventWithName:TKIMEmotionClickEvent userInfo:dic];
    }
}


/**
 *  @Author lpm, 15-11-19 17:11:40
 *
 *  @brief  发送消息
 */
-(void)send
{
    [self routerEventWithName:TKIMEmotionSendEvent userInfo:nil];
}

@end
